

//Variáveis globais para esta tela


mainComponents = new Array();
	
function save(){
	
	if(!validate(["compname","complogin","comppass"])){
		return;
	}
	
	var xmlData = "<?xml version=\"1.0\"?>";
	
	xmlData += "<ROOT>";
	xmlData += "<FORM value=\""+FORM+"\"> </FORM>";
	xmlData += "<ACTION value=\"INSERT\"></ACTION>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+compname.getId()+"\" value=\""+compname.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+complogin.getId()+"\" value=\""+complogin.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+comppass.getId()+"\" value=\""+comppass.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+comptype.getId()+"\" value=\""+comptype.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "</ROOT>";

	var conn = new ClientConnector();
	conn.setCallBack(function(code){
		eval(code);
	});
	
	conn.send(xmlData);
}

function apply(){
	if(isNew){
		save();
	}else{
		update();
	}
}

function cancel(){
	var imgNew = getById("img-new-user");
	imgNew.style.display = "none";
	isNew = false;
}


function showNewHint(show){
	var imgNew = getById("img-new-user");
	if(show == true){
		imgNew.style.display = "inline";
	}else{
		imgNew.style.display = "none";
	}
}

function setNew(){
	isNew = true;
	showNewHint(true);
	clearFields();
	getById("btApply").disabled = false;
}

function update(){
	
	if(!validate(["compname","complogin","comppass"])){
		return;
	}
	
	var xmlData = "<?xml version=\"1.0\"?>";
	xmlData += "<ROOT>";
	xmlData += "<FORM value=\""+FORM+"\"> </FORM>";
	xmlData += "<ACTION value=\"UPDATE\"></ACTION>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+compname.getId()+"\" value=\""+compname.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+complogin.getId()+"\" value=\""+complogin.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+comppass.getId()+"\" value=\""+comppass.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+comptype.getId()+"\" value=\""+comptype.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "</ROOT>";
	
    var conn = new ClientConnector();
	
	conn.setCallBack(function(code){
		eval(code);
	});
	
	conn.send(xmlData);

	clearFields();
}

function remove(){
	var xmlData = "<?xml version=\"1.0\"?>";
	
	xmlData += "<ROOT>";
	xmlData += "<FORM value=\""+FORM+"\"> </FORM>";
	xmlData += "<ACTION value=\"DELETE\"></ACTION>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+complogin.getId()+"\" value=\""+complogin.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "</ROOT>";
    var conn = new ClientConnector();
	
	conn.setCallBack(function(code){
		eval(code);
	});
	
	conn.send(xmlData);
	
	clearFields();

}

function select(){
	isNew = false;
	
	if(!validate(["complogin"])){
		return;
	}
	
	showNewHint(false);
	
	var xmlData = "<?xml version=\"1.0\"?>";
	
	xmlData += "<ROOT>";
	xmlData += "<FORM value=\""+FORM+"\"> </FORM>";
	xmlData += "<ACTION value=\"SELECT\"></ACTION>";
	xmlData += "<COMPONENT>";
	xmlData += "<DATA id=\""+complogin.getId()+"\" value=\""+complogin.getValue()+"\" > </DATA>";
	xmlData += "</COMPONENT>";
	xmlData += "</ROOT>";
    var conn = new ClientConnector();
	
	conn.setCallBack(function(code){
		eval(code);
	});
	
	conn.send(xmlData);
	
	getById("btApply").disabled = false;
}

function clearFields(){
	compname.setValue("");
	complogin.setValue("");
	comptype.setValue("OPERADOR");
	comppass.setValue("");	
}


function initialize(){
	compname = new EditBox("name", "$name");
	compname.setDescription("Nome");
	mainComponents.push(compname);
	
	complogin = new EditBox("login", "$login");
	complogin.setDescription("Login");
	mainComponents.push(complogin);
	
	comppass = new EditBox("pass", "$pass");
	comppass.setDescription("Senha");
	mainComponents.push(comppass);
	
	comptype = new ComboBox("type", "$type");
	comptype.setDescription("Tipo de usuário");
	mainComponents.push(comptype);
	
	comptype.addOption("OPERADOR");
	comptype.addOption("ADMINISTRADOR");
	mainComponents.push(comptype);
	
	compname.render();
	complogin.render();
	comppass.render();
	comptype.render();
}



function validate(fields){
	
	var hasMissingFields = false;
	var missingFields = [];
	
	for ( var i = 0; i < fields.length; i++) {
		var component = eval(fields[i]);
		
		var hasValue = component.getValue() != "";
		if(hasValue == false){
			hasMissingFields = true;
			component.htmlElement.onchange = new Function("changeInvalidComponent(\""+component.getId()+"\",\"mainComponents\");");
			component.setInvalid(true);
			missingFields.push(component);
		}
	}

	if(hasMissingFields == true){
		var msg = "Os seguintes campos devem ser preenchidos:  \n";
		for ( var j = 0; j < missingFields.length; j++) {
			var cmp = missingFields[j];
			msg += " - " + cmp.getDescription() + " - \n";
		}
		alert(msg);
		return false;
	}
	return true;
}
